﻿using System;
using System.Data;
using System.Windows.Forms;
using RescueApp.Data;
using RescueApp.UI.Common;

namespace RescueApp.UI
{
    public partial class frmViewRescueDetails : Form
    {
        private RescueContext db;

        public frmViewRescueDetails()
        {
            InitializeComponent();
        }

        public frmViewRescueDetails(RescueContext db)
        {
            InitializeComponent();
            this.db = db;
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            DateTime startDate = dtp_startDate.Value;
            DateTime endDate = dtp_endDate.Value;

            string sql = @"SELECT 
        a.RescueID,
        a.VehicleLicensePlate AS 故障车车牌,
        a.CarType AS 故障车型,
        a.ApplicationDate AS 申请日期,
        d.DispatchDriver AS 救援司机,
        d.DispatchVehicleLicense AS 救援车辆车牌,
        d.ArrivalTime AS 到达时间,
        d.RescueCost AS 救援费用
    FROM [dbo].[RescueApply] a
    LEFT JOIN [dbo].[RescueDispatch] d ON a.RescueID = d.RescueID
    WHERE a.ApplicationDate BETWEEN @StartDate AND @EndDate";

            IDictionary<string, object> dict = new Dictionary<string, object>
    {
        { "@StartDate", startDate },
        { "@EndDate", endDate }
    };

            DataTable dt = db.SqlDataTable(sql, dict);
            dataGridView1.DataSource = dt;
            dataGridView1.Refresh();

            lbl_statistics.Text = $"查询结果：共 {dt.Rows.Count} 条记录";
        }
    }
}